package me.bincker.formwork.web.core.config

import me.bincker.formwork.web.core.filter.UsernamePasswordLoginFilter
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpMethod
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter

/**
 * 安全配置
 */
@Configuration
class WebSecurityConfiguration: WebSecurityConfigurerAdapter() {
    override fun configure(http: HttpSecurity) {
        http
                .authorizeRequests()
                .antMatchers(HttpMethod.POST, "/login").permitAll()
                .and()
//                打算使用localStorage防御csrf，即不通过cookies存储信息
                .csrf().disable()
        http
                .addFilterAt(
                        UsernamePasswordLoginFilter().apply { setAuthenticationManager(authenticationManager()) },
                        UsernamePasswordAuthenticationFilter::class.java
                )
    }
}